{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "==================================================\n",
      "满足“下午成交量 > 上午成交量”的天数：95\n",
      "其中第二天高开的次数：51\n",
      "其中第二天上涨的次数：56\n",
      "第二天高开的概率：53.68%\n",
      "第二天上涨的概率：58.95%\n",
      "==================================================\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# === 1. 读取 CSV 文件 ===\n",
    "file_path = \"../../../data/30年国债ETF博时_511130_120分_20240328_20250530.csv\"\n",
    "\n",
    "# 尝试读取文件\n",
    "try:\n",
    "    df = pd.read_csv(file_path, encoding=\"utf-8\")\n",
    "except UnicodeDecodeError:\n",
    "    df = pd.read_csv(file_path, encoding=\"gbk\")\n",
    "\n",
    "# === 2. 预处理 ===\n",
    "df['时间'] = pd.to_datetime(df['时间'])\n",
    "df = df.sort_values(by='时间').reset_index(drop=True)\n",
    "\n",
    "grouped = df.groupby(df['时间'].dt.date)\n",
    "dates = sorted(grouped.groups.keys())\n",
    "\n",
    "# === 3. 回测逻辑 ===\n",
    "total_cases = 0\n",
    "high_open_count = 0\n",
    "close_up_count = 0\n",
    "\n",
    "for i in range(len(dates) - 1):\n",
    "    date_today = dates[i]\n",
    "    date_next = dates[i + 1]\n",
    "\n",
    "    try:\n",
    "        data_today = grouped.get_group(date_today)\n",
    "        data_next = grouped.get_group(date_next)\n",
    "    except KeyError:\n",
    "        continue\n",
    "\n",
    "    row_am = data_today[data_today['时间'].dt.time == pd.to_datetime(\"11:30\").time()]\n",
    "    row_pm = data_today[data_today['时间'].dt.time == pd.to_datetime(\"15:00\").time()]\n",
    "    row_next_am = data_next[data_next['时间'].dt.time == pd.to_datetime(\"11:30\").time()]\n",
    "    row_next_pm = data_next[data_next['时间'].dt.time == pd.to_datetime(\"15:00\").time()]\n",
    "\n",
    "    if row_am.empty or row_pm.empty or row_next_am.empty or row_next_pm.empty:\n",
    "        continue\n",
    "\n",
    "    volume_am = row_am.iloc[0]['总手']\n",
    "    volume_pm = row_pm.iloc[0]['总手']\n",
    "\n",
    "    if volume_pm > volume_am:\n",
    "        total_cases += 1\n",
    "\n",
    "        prev_close = row_pm.iloc[0]['收盘']\n",
    "        next_open = row_next_am.iloc[0]['开盘']\n",
    "        next_close = row_next_pm.iloc[0]['收盘']\n",
    "\n",
    "        if next_open > prev_close:\n",
    "            high_open_count += 1\n",
    "        if next_close > prev_close:\n",
    "            close_up_count += 1\n",
    "\n",
    "# === 4. 输出结果 ===\n",
    "print(\"=\" * 50)\n",
    "print(f\"满足“下午成交量 > 上午成交量”的天数：{total_cases}\")\n",
    "print(f\"其中第二天高开的次数：{high_open_count}\")\n",
    "print(f\"其中第二天上涨的次数：{close_up_count}\")\n",
    "\n",
    "if total_cases > 0:\n",
    "    prob_high_open = high_open_count / total_cases * 100\n",
    "    prob_close_up = close_up_count / total_cases * 100\n",
    "    print(f\"第二天高开的概率：{prob_high_open:.2f}%\")\n",
    "    print(f\"第二天上涨的概率：{prob_close_up:.2f}%\")\n",
    "else:\n",
    "    print(\"无满足条件的数据\")\n",
    "print(\"=\" * 50)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
